package cn.zzu.Etrip.twoday.view;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.etrip.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import cn.zzu.Etrip.twoday.service.lightAndPrice;

/**
 * Created by ASUS on 2018/5/24.
 */

public class NavigateFragment extends Fragment {

    private TextView zhenzhou, xinxiang, shangqiu, xinyang, anyang, luoyang, xuchang, luohe, kaifeng, sanmenxia;
    private ImageView zhenzhouImage, xinxiangImage, shangqiuImage, xinyangImage, anyangIamge, luoyangImage, xuchangImage, luoheImage, kaifengImage, sanmenxiaImage;

    //当调用Fragment时就开始准备创建这个布局之中的组件
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        return inflater.inflate(R.layout.two_navigate_fragment, container, false);
    }

    //当组件创建之后就开始执行的事件
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findViewAndSetListener(view);
    }

    public void findViewAndSetListener(View view) {
        ArrayList<TextView> textViews = new ArrayList();

        zhenzhou = view.findViewById(R.id.two_zhengzhou);
        xinxiang = view.findViewById(R.id.two_xinxiang);
        shangqiu = view.findViewById(R.id.two_shangqiu);
        xinyang = view.findViewById(R.id.two_xinyang);
        anyang = view.findViewById(R.id.two_anyang);
        luoyang = view.findViewById(R.id.two_luoyang);
        xuchang = view.findViewById(R.id.two_xuchang);
        luohe = view.findViewById(R.id.two_luohe);
        kaifeng = view.findViewById(R.id.two_kaifeng);
        sanmenxia = view.findViewById(R.id.two_sanmenxia);
        textViews.add(zhenzhou);
        textViews.add(xinxiang);
        textViews.add(shangqiu);
        textViews.add(xinyang);
        textViews.add(anyang);
        textViews.add(luoyang);
        textViews.add(xuchang);
        textViews.add(luohe);
        textViews.add(kaifeng);
        textViews.add(sanmenxia);

        ArrayList<Integer> idArr = new ArrayList();
        zhenzhouImage = view.findViewById(R.id.two_zhengzhouImage);
        xinxiangImage = view.findViewById(R.id.two_xinxiangImage);
        shangqiuImage = view.findViewById(R.id.two_shangqiuImage);
        xinyangImage = view.findViewById(R.id.two_xinyangImage);
        anyangIamge = view.findViewById(R.id.two_anyangImage);
        luoyangImage = view.findViewById(R.id.two_luoyangImage);
        xuchangImage = view.findViewById(R.id.two_xuchangImage);
        luoheImage = view.findViewById(R.id.two_luoheImage);
        kaifengImage = view.findViewById(R.id.two_kaifengImage);
        sanmenxiaImage = view.findViewById(R.id.two_sanmenxiaImage);
        idArr.add(R.id.two_zhengzhouImage);
        idArr.add(R.id.two_xinxiangImage);
        idArr.add(R.id.two_shangqiuImage);
        idArr.add(R.id.two_xinyangImage);
        idArr.add(R.id.two_anyangImage);
        idArr.add(R.id.two_luoyangImage);
        idArr.add(R.id.two_xuchangImage);
        idArr.add(R.id.two_luoheImage);
        idArr.add(R.id.two_kaifengImage);
        idArr.add(R.id.two_sanmenxiaImage);

        HashMap<Integer, ImageView> map = new HashMap<Integer, ImageView>();
        map.put(R.id.two_zhengzhouImage, zhenzhouImage);
        map.put(R.id.two_xinxiangImage, xinxiangImage);
        map.put(R.id.two_shangqiuImage, shangqiuImage);
        map.put(R.id.two_xinyangImage, xinyangImage);
        map.put(R.id.two_anyangImage, anyangIamge);
        map.put(R.id.two_luoyangImage, luoyangImage);
        map.put(R.id.two_xuchangImage, xuchangImage);
        map.put(R.id.two_luoheImage, luoheImage);
        map.put(R.id.two_kaifengImage, kaifengImage);
        map.put(R.id.two_sanmenxiaImage, sanmenxiaImage);
        zhenzhou.setTextColor(getResources().getColor(R.color.colorAccent));
        //为每一个按钮添加一个监听器
        for (int a = 0; a < textViews.size(); a++) {
            textViews.get(a).setOnClickListener(new ViewListener(idArr.get(a), map));
        }

    }
    //定义一个外部类，实现监听器方法
    class ViewListener implements View.OnClickListener {
        private int values;
        private HashMap<Integer, ImageView> map;

        public ViewListener(int values, HashMap<Integer, ImageView> map) {
            this.values = values;
            this.map = map;
        }

        public void onClick(View v) {
            Collection<ImageView> imageViews = map.values();
            for (ImageView ima : imageViews) {
                ima.setImageResource(0);
            }
            ImageView realImage = map.get(values);
            realImage.setImageResource(R.mipmap.myinfo_icon_hongqi);
            //调用LightAndprice的类,通过id值来获得相应的List集合。
            List lAndPList = lightAndPrice.returnMap().get(values);
            openContentFragment(lAndPList);
        }

        //打开另一个fragment的事件
        public void openContentFragment(List lAndPList) {
            ContentFragment contentFragment = ContentFragment.returnContentFragment((String[]) lAndPList.get(1), (String[]) lAndPList.get(2), (Integer[]) lAndPList.get(0));
            ////动态设置fragment内部的组件内容
            getActivity().getFragmentManager().beginTransaction().add(R.id.content,contentFragment).commitAllowingStateLoss();
        }


    }
}













